﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using LyyCMS.Web.Models;
using LyyCMS.Core;
using Microsoft.EntityFrameworkCore;


namespace LyyCMS.Web.Controllers
{
    public class HomeController : Controller
    {
        //LyyCMSDbContextFactory _lyyCMSDbContextFactory;
        private readonly LyyCMSEntities _lyyCMSEntities;
        private readonly ILogger<HomeController> _logger;
        private readonly int pagesize = 5; //页码  应该设置为const

        public HomeController(ILogger<HomeController> logger, LyyCMSEntities lyyCMSEntities)
        {
            _logger = logger;
            _lyyCMSEntities = lyyCMSEntities;
        }

        public IActionResult Index(int page=1)
        {
            //默认不加载 Comments数据， 注意 需要区分大小写,comments会报错

            List<Core.Models.Blog> blogswithcomment = _lyyCMSEntities.Blogs.
                Include("Comments")
                .OrderByDescending(x=>x.Create_At)   //创建时间倒序
                .Skip((page - 1) * pagesize)         //截取数据
                .Take(pagesize)                      //取pagesize条数据
                .ToList();


            int count = 1; //总页数
            count = _lyyCMSEntities.Blogs.Count() / 5;
            count += _lyyCMSEntities.Blogs.Count() % 5 > 0 ? 1 : 0;

            ViewBag.pagecount = count;
            ViewBag.currentpage = page;

            ViewBag.preview = page > 1 ? page - 1 : page;
            ViewBag.nextpage = page >= count ? count : count + 1;

            return View(blogswithcomment);
        }

        // GET: Blogs/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var blog = await _lyyCMSEntities.Blogs.Include("Comments")
                .FirstOrDefaultAsync(m => m.Id == id);
            if (blog == null)
            {
                return NotFound();
            }

            return View(blog);
        }

        public async Task<IActionResult> CateList(int? id,int page =1)
        {
            if (id == null)
            {
                return NotFound();
            }
            var cate = (LyyCMS.Core.Models.BlogCategory)Enum.ToObject(typeof(LyyCMS.Core.Models.BlogCategory), id);

            List<Core.Models.Blog> blogswithcomment = await _lyyCMSEntities.Blogs.
               Include("Comments")
               .Where(x=>x.Category==cate)
               .OrderByDescending(x => x.Create_At)   //创建时间倒序
               .Skip((page - 1) * pagesize)         //截取数据
               .Take(pagesize)                      //取pagesize条数据
               .ToListAsync();


            int count = 1; //总页数
            count = _lyyCMSEntities.Blogs.Where(x=>x.Category==cate).Count() / 5;
            count += _lyyCMSEntities.Blogs.Where(x => x.Category == cate).Count() % 5 > 0 ? 1 : 0;

            ViewBag.pagecount = count;
            ViewBag.currentpage = page;

            ViewBag.preview = page > 1 ? page - 1 : page;
            ViewBag.nextpage = page >= count ? count : count + 1;

            ViewBag.cate = cate;

            return View(blogswithcomment);

        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
